//
//  BaseLoginVC.swift
//  SlaughterClient
//
//  Created by Zhouweibin on 2018/1/27.
//  Copyright © 2018年 zwb. All rights reserved.
//

import UIKit

class BaseLoginVC: UIViewController {
    
    func getCheckedImgName() -> String {
        return "check_ed_orange"
    }
    func getLoginbgImgName() -> String {
        return "login_bg_orange"
    }
    
    lazy var bgImage: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: getLoginbgImgName())
        return imageView
    } ()
 
    lazy var hiddenPwd: SeePasswordTool = {
        let v = SeePasswordTool()
        return v
    } ()
 
    
    lazy var usernameLabel: ImageTextField = {
        let label = ImageTextField(image: UIImage(named: "mobile_w"), placeholder: "请输入账户")
         label.textField.clearsOnBeginEditing = true
        label.textField.clearButtonMode = .always
        label.textField.autocapitalizationType = .none
        return label
    } ()
    
    lazy var passwordLabel: ImageTextField = {
        let label = ImageTextField(image: UIImage(named: "password_w"), placeholder: "请输入密码")
         label.textField.isSecureTextEntry = true
        label.textField.autocapitalizationType = .none
         return label
    } ()
    var zoneSegV:ZoneSegView = {
       let v = ZoneSegView()
//        v.backgroundColor = .clear
        v.isUserInteractionEnabled = true
        v.setCollecDataSource(arr: [SegmenttData(id: "", nm: "密码登录"),SegmenttData(id: "", nm: "验证码登录")])
        v.isHidden = true
        return v
    }()
    lazy var loginButton: UIButton = {
        let v = UIButton()
        v.layer.cornerRadius = 23
        v.backgroundColor = Theme.primaryColor()
        v.setTitle("登录", for: UIControl.State())
        v.setTitleColor(.white, for: .normal)
        v.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        return v
    } ()
    
    lazy var otherTypeV: OtherTypeView = {
        let v = OtherTypeView()
        v.isHidden = true
        return v
    } ()
    
    lazy var rememberButton: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "check_un"), for: UIControl.State())
        return button
    } ()
    
    lazy var rememberLabel: UILabel = {
        let label = UILabel()
        label.text = "记住密码"
        label.textColor = UIColor(kshex: "#8991B0")
        label.font = UIFont.systemFont(ofSize: 14)
        return label
    } ()
    
    lazy var autoButton: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "check_un"), for: UIControl.State())

        return button
    } ()
    
    lazy var autoLabel: UILabel = {
        let label = UILabel()
        label.text = "自动登录"
        label.textColor = UIColor(kshex: "#8991B0")
        label.font = UIFont.systemFont(ofSize: 14)
        return label
    } ()
    
    
 
    lazy var registerBtn: UIButton = {
        let button = UIButton()
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitleColor(UIColor(kshex: "8991B0"), for: .normal)
        button.setTitle("立即注册", for: .normal)
        button.titleLabel?.textAlignment = .right
   
        return button
    } ()
 
  
  
    /// 改变记住密码图标状态
     var isRememberPwd = false {
        didSet {
            if isRememberPwd {
                rememberButton.setImage(UIImage(named: getCheckedImgName()), for: UIControl.State())
            } else {
                rememberButton.setImage(UIImage(named: "check_un"), for: UIControl.State())
            }
        }
    }
    
    /// 改变自动登录图标状态
     var isAutoLogin = false {
        didSet {
            if isAutoLogin {
                autoButton.setImage(UIImage(named: getCheckedImgName()), for: UIControl.State())
            } else {
                autoButton.setImage(UIImage(named: "check_un"), for: UIControl.State())
            }
        }
    }
  
    var isLogOut = false
    var leading:CGFloat = 42
    var logoStr:UILabel = {
        let v = UILabel()
        v.numberOfLines = 2
        v.text = "您好,\n欢迎使用诸友安"
        v.textColor = Theme.primaryColor()
        v.font = UIFont.boldSystemFont(ofSize: 29)
        return v
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
 
        setupSunviews()
        addConstraints()
        addEvent()
 
 
        
        
        
 
        
    }
  
    
    private func setupSunviews() {
        view.addSubview(bgImage)
         view.addSubview(logoStr)
        view.addSubview(usernameLabel)
        view.addSubview(passwordLabel)
        
        
        
        hiddenPwd.relevanceField = passwordLabel.textField
        view.addSubview(hiddenPwd)
        
        hiddenPwd.snp.makeConstraints { (maker) in
            maker.height.width.equalTo(25)
            maker.centerY.equalTo(passwordLabel)
            maker.right.equalTo(passwordLabel).offset(-12)
        }
        view.addSubview(loginButton)
        
        view.addSubview(otherTypeV)
        view.addSubview(rememberLabel)
        view.addSubview(rememberButton)
        self.view.addSubview(registerBtn)
      
        view.addSubview(autoLabel)
        view.addSubview(autoButton)
        view.addSubview(zoneSegV)
        zoneSegV.collecSelected = { indx ,data  in
            if indx == 1 {
                KSToast("验证码功能升级中")
                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.8) {
                    self.zoneSegV.selectIndx = 0
                    self.zoneSegV.collectionView.reloadData()
                }
                
            }
        }
 
    }
   
    private func addConstraints() {
        bgImage.snp.makeConstraints { (maker) in
            maker.edges.equalToSuperview()
        }
        
 
        
        
        logoStr.snp.makeConstraints { (maker) in
            maker.top.equalTo(self.view).offset(KSGlobal.screenH * 0.15)
            maker.left.equalToSuperview().offset(30)
        }
 
        zoneSegV.snp.makeConstraints { (maker) in
 
            maker.height.equalTo(36)
            maker.width.equalTo(203)

            maker.left.equalTo(leading - 6)
            maker.bottom.equalTo(usernameLabel.snp.top).offset(-12)
        }
        
        usernameLabel.snp.makeConstraints { (maker) in
 
            maker.height.equalTo(46)
            maker.left.equalTo(leading)
            maker.right.equalTo(-leading)
        }
        
        passwordLabel.snp.makeConstraints { (maker) in
            
            maker.centerY.equalToSuperview().offset(-10)
           
            
            maker.top.equalTo(usernameLabel.snp.bottom).offset(25)
            maker.height.equalTo(46)
            maker.left.equalTo(leading)
            maker.right.equalTo(-leading)
        }
 
        rememberButton.snp.makeConstraints { (maker) in
            maker.left.equalTo(passwordLabel.snp.left)
            maker.top.equalTo(passwordLabel.snp.bottom).offset(28)
            maker.size.equalTo(CGSize(width: 20, height: 20))
        }
        
        rememberLabel.snp.makeConstraints { (maker) in
            maker.left.equalTo(rememberButton.snp.right).offset(2)
            maker.centerY.equalTo(rememberButton)
        }
//        
        autoButton.snp.makeConstraints { (maker) in
            maker.left.equalTo(rememberLabel.snp.right).offset(15)
            maker.centerY.equalTo(rememberButton)
            maker.size.equalTo(CGSize(width: 20, height: 20))
            maker.centerY.equalTo(rememberButton)
        }
        
        autoLabel.snp.makeConstraints { (maker) in
//            maker.centerX.equalTo(self.view).offset(8)
            maker.left.equalTo(autoButton.snp.right).offset(2)
            maker.centerY.equalTo(rememberButton)
        }
        registerBtn.snp.makeConstraints { make in
            make.right.equalTo(self.view).offset(-26)
            make.centerY.equalTo(rememberLabel)
            make.width.equalTo(80)
            make.height.equalTo(28)
        }
   
        loginButton.snp.makeConstraints { (maker) in
            maker.top.equalTo(rememberButton.snp.bottom).offset(20)
            maker.height.equalTo(46)
            maker.left.equalTo(leading)
            maker.right.equalTo(-leading)
        }
 
        otherTypeV.snp.makeConstraints { (maker) in
            maker.height.equalTo(155)
            maker.bottom.left.right.equalToSuperview()
        }
 
    }
    
    private func addEvent() {
        registerBtn.addTouchUpInSideAction { _ in
            let nav = BaseNavigationController()
            let vc = TrickRegisterVC()
            nav.viewControllers = [vc]
            self.present_FullScreen(to: nav)
        }
    }
 
   
  
}
 

class ImageTextField: UIView {
    lazy var line: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(kshex: "f1f1f1")
        return v
    } ()
    lazy var imageView: UIImageView = {
        let image = UIImageView()
        return image
    } ()
    
    lazy var textField: UITextField = {
        let textField = UITextField()
        textField.textColor = UIColor(kshex: "111111")
         return textField
    } ()
    
    convenience init(image: UIImage?, placeholder: String) {
        self.init(frame: CGRect.zero)
        imageView.image = image
        textField.placeholder = placeholder
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(imageView)
        addSubview(textField)
        addSubview(line)

        imageView.snp.makeConstraints { (maker) in
            maker.size.equalTo(CGSize(width: 25, height: 25))
            maker.left.equalTo(12)
            maker.centerY.equalToSuperview()
        }
        
        textField.snp.makeConstraints { (maker) in
            maker.left.equalTo(imageView.snp.right).offset(8)
            maker.right.bottom.top.equalToSuperview()
        }
        line.snp.makeConstraints { (maker) in
            maker.left.right.equalTo(self)
            maker.height.equalTo(1)
            maker.bottom.equalToSuperview()
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
class OtherTypeView: UIView {
    lazy var lineL: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(kshex: "A0A7C2")
        return v
    } ()
    lazy var lineR: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor(kshex: "A0A7C2")
        return v
    } ()
    lazy var descLab: UILabel = {
        let v = UILabel()
        v.text = "其他方式登录"
        v.font = UIFont.systemFont(ofSize: 10)
        v.textColor = UIColor(kshex: "#A0A7C2")
        return v
    } ()
    
    var faceLogin : UIButton = {
        let v = UIButton()
    
        v.setBackgroundImage(UIImage(named: "face_id"), for: .normal)
        return v
    }()
 
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(descLab)
        addSubview(faceLogin)
        addSubview(lineL)
        addSubview(lineR)
        descLab.snp.makeConstraints { (maker) in
            maker.centerX.equalToSuperview()
            maker.top.equalToSuperview()
        }
        lineR.snp.makeConstraints { (maker) in
            maker.width.equalTo(65)
            maker.height.equalTo(1)
            maker.left.equalTo(descLab.snp.right).offset(10)
            maker.centerY.equalTo(descLab)
        }
        lineL.snp.makeConstraints { (maker) in
            maker.width.equalTo(65)
            maker.right.equalTo(descLab.snp.left).offset(-10)
            maker.height.equalTo(1)
            maker.centerY.equalTo(descLab)
        }
        faceLogin.snp.makeConstraints { (maker) in
            maker.centerX.equalToSuperview()
            maker.top.equalTo(descLab.snp.bottom).offset(18)
            maker.width.height.equalTo(45)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
 
